# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=librsvg
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=2.56.3
pkgrel=1
pkgdesc="SVG rendering library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://wiki.gnome.org/Projects/LibRsvg"
license=("spdx:LGPL-2.1-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3: for rsvg-view-3")
options=('staticlibs' 'strip')
source=("https://download.gnome.org/sources/librsvg/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "0001-configure-set-pass-prefix-to-pkg-config-when-retriev.patch")
sha256sums=('5a328048a02d014645cd27f61140f4e0b11280fb2c7f2a21864fe0c59ac1ce88'
            '189eec6486c9ef6cf1071af9750405bc41e16ae3a55ad663435e574369fec915')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/0001-configure-set-pass-prefix-to-pkg-config-when-retriev.patch"

  cargo fetch --locked

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # html documentation doesn't install from out of tree
  cp -rf "${srcdir}/${_realname}-${pkgver}/doc" "${srcdir}/build-${MSYSTEM}/doc"

  # https://github.com/msys2/MINGW-packages/pull/13622#issuecomment-1285820970
  export WINAPI_NO_BUNDLED_LIBRARIES=1

  export lt_cv_deplibs_check_method='pass_all'

  # an argument passed to glib-mkenums starts with a c++ comment
  export MSYS2_ARG_CONV_EXCL="/*"
  # so the unix path gets preserved when passed through pkg-config
  MSYS2_ARG_CONV_EXCL="$MSYS2_ARG_CONV_EXCL;--define-variable=prefix"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-introspection \
    --enable-vala \
    --enable-silent-rules

  make
}

package_librsvg() {
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-2.40")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-2.40")
  make -C "${srcdir}/build-${MSYSTEM}" DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"

  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_librsvg-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
